Skip to content

Fix Java reformatter for empty records and nested lambdas#9421

Open
neilcsmith-net wants to merge 2 commits into
apache:masterfrom
neilcsmith-net:reformat-fixes
Open

Fix Java reformatter for empty records and nested lambdas#9421
neilcsmith-net wants to merge 2 commits into
apache:masterfrom
neilcsmith-net:reformat-fixes

Conversation

@neilcsmith-net
Copy link
Copy Markdown
Member

Fixes for two niggles in the reformatter - empty record bodies and nested lambda continuation indents.

Empty records

Where a record has an empty body with no whitespace -

record Foo(int x) {}

Stop the reformatter turning this into -

record Foo(int x) {

}

This change keeps the original code intact while reformatting all non-empty bodies as before. The change only affects the default same-line brace placement. The other modes are somewhat broken with records already.

NB. The classLeftBracePlacement method is removed and the code added to the record handling. Despite its name, this isn't used for non-record classes anyway, and while the intention might have been to allow code reuse, the method isn't correct for this.

Nested lambda continuations

Where code has nested lambdas, such as -

public static void main(String[] args) {
    List<String> lines = Stream.of(args)
            .flatMap(s -> s.lines()
                    .filter(l -> !l.isBlank())
            ).toList();
}

Stop the reformatter from flattening the continuation indents -

public static void main(String[] args) {
    List<String> lines = Stream.of(args)
            .flatMap(s -> s.lines()
            .filter(l -> !l.isBlank())
            ).toList();
}

This behaviour is already correct if wrapAfterLambdaArrow is set, but not in the default case.

Special case reformatting of empty body records without whitespace
(eg. record Foo(int x) {} ) to not insert lines and keep body as is.
The behaviour is only changed for default same-line brace handling.
Fix continuation indenting with nested lambdas being the same by
not specifying a specific indent. This already works correctly
when wrapAfterLambdaArrow is set. This fixes the default behaviour.
@neilcsmith-net neilcsmith-net added this to the NB31 milestone Jun 1, 2026
@neilcsmith-net neilcsmith-net requested a review from mbien June 1, 2026 12:10
@neilcsmith-net neilcsmith-net added the Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) label Jun 1, 2026
Copy link
Copy Markdown
Contributor

@dbalek dbalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable. Thanks.

@matthiasblaesing
Copy link
Copy Markdown
Contributor

Thank you! Especially the record formatting annoyed the hell out of me, so great to see this fixed!

For the same line setting this looks what I would expect, but "Class Declaration: New Line" causes a strange formatting (here record in contrast to the class). I think the opening brace for the record should be in the same column as the closing one:

grafik

For "New Line Half Indented" the opening brace is to far right and the closing one is not far enough on the right:

grafik

For "New Line Indented" the situation is the same, just the opening brace is further to far right.

grafik

@neilcsmith-net
Copy link
Copy Markdown
Member Author

Thanks @matthiasblaesing Yes, as I said, the other modes are somewhat broken already. This should be the same behaviour as recent releases? I've tried not to touch them for now. If it's not a regression I would prefer to consider as a follow up issue. There's a bit more to think about what to do with them if they're empty (new line may be odd for that), and we need some tests for those other modes.

@matthiasblaesing
Copy link
Copy Markdown
Contributor

@neilcsmith-net a sorry I overlooked that remark. And yes, the brokenness is identical before and after the change, so ignore that part of my comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants